package com.gooddare.view.component 
{
	import com.gooddare.controller.ApplicationFacade;
	import com.gooddare.locator.AssetsLocator;
	import com.gooddare.model.ModelLocator;
	import com.riatimes.mvc.view.BaseComponent;
	import com.riatimes.util.Reflection;
	import com.riatimes.util.UIUtil;
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.ProgressEvent;
	import flash.net.URLRequest;
	import com.greensock.TweenMax;
	import flash.text.TextField;
	/**
	 * ...
	 * @author Lyleff
	 */
	public class HomepictureComponent extends BaseComponent
	{
		private var backPanel:Sprite;
		private var addImg:Sprite;
		private var imgBack:Sprite;
		private var msgPanel:Sprite;
		private var loader:Loader;
		private var leftArrow:Sprite;
		private var rightArrow:Sprite;
		private var id:int;
		private var lb:Boolean = false;
		private var rb:Boolean = false;
		
		private var lbv:Sprite;//竖条
		private var lbc:Sprite;//横条
		private var loadingMask:Sprite;
		
		private var imgWidth:Number;
		private var imgHeight:Number;
		private var backWidth:Number;
		private var backHeight:Number;
		private var msgTxt:TextField;
		
		private var bLoading:Boolean = true;
		
		public function HomepictureComponent() 
		{
			this.addEventListener(Event.ADDED_TO_STAGE, __addToStageHandle);
		}
		private function __addToStageHandle(e:Event):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, __addToStageHandle);
		}
		override public function initUI():void {
			backPanel = Reflection.createDisplayObjectInstance(AssetsLocator.ASSETS_IMGPLAYER_UI) as Sprite;
			this.addChild(backPanel);
			addImg = UIUtil.getElement(backPanel, "addImg") as Sprite;
			imgBack = UIUtil.getElement(backPanel, "imgBack") as Sprite;
			msgPanel = UIUtil.getElement(backPanel, "msgPanel") as Sprite;
			leftArrow = UIUtil.getElement(backPanel, "leftArrow") as Sprite;
			rightArrow = UIUtil.getElement(backPanel, "rightArrow") as Sprite;
			loadingMask = UIUtil.getElement(backPanel, "loadingMask") as Sprite;
			msgTxt = UIUtil.getElement(msgPanel, "msgTxt") as TextField;
			
			lbv = UIUtil.getElement(backPanel, "lbv") as Sprite;
			lbc = UIUtil.getElement(backPanel, "lbc") as Sprite;
			lbv.mask = loadingMask;
			lbv.mask = loadingMask;
			
			lbv.height = imgHeight=imgBack.height;
			lbc.width = imgWidth = imgBack.width;
			loadingMask.width = imgWidth;
			loadingMask.height = imgHeight;
			lbv.x = -imgBack.width / 2;
			lbc.y = -imgBack.height / 2 + 5;
			
			leftArrow.buttonMode = rightArrow.buttonMode = true;
			
			msgPanel.alpha = 1;
			
			this.x = 482;
			TweenMax.to(this, .5, { y:297.5+15 } );
			bLoading = true;
		}
		override public function initEvent (b:Boolean = true) : void {
			if (b) {
				addImg.addEventListener(MouseEvent.CLICK, __mouseClickEventHandle);//下面有点击移出
				imgBack.addEventListener(MouseEvent.CLICK, __mouseClickEventHandle);
				
				leftArrow.addEventListener(MouseEvent.CLICK, __arrowClickEventHandle);
				rightArrow.addEventListener(MouseEvent.CLICK, __arrowClickEventHandle);
				
				leftArrow.addEventListener(MouseEvent.ROLL_OVER, __arrowRollEventHandle);
				rightArrow.addEventListener(MouseEvent.ROLL_OVER, __arrowRollEventHandle);
				leftArrow.addEventListener(MouseEvent.ROLL_OUT, __arrowRollEventHandle);
				rightArrow.addEventListener(MouseEvent.ROLL_OUT, __arrowRollEventHandle);
			}else {
				leftArrow.removeEventListener(MouseEvent.CLICK, __arrowClickEventHandle);
				rightArrow.removeEventListener(MouseEvent.CLICK, __arrowClickEventHandle);
			}
		}
		private function __arrowRollEventHandle(e:MouseEvent):void 
		{
			switch(e.type) {
				case MouseEvent.ROLL_OVER:
					if (e.currentTarget == leftArrow) {
						TweenMax.to(leftArrow["arrow"],0.5,{glowFilter:{color:0x000000,blurX:20,blurY:20}})
					}else if (e.currentTarget == rightArrow) {
						TweenMax.to(rightArrow["arrow"],0.5,{glowFilter:{color:0x000000,blurX:20,blurY:20}})
					}
				break;
				case MouseEvent.ROLL_OUT:
					if (e.currentTarget == leftArrow) {
						TweenMax.to(leftArrow["arrow"],0.5,{glowFilter:{color:0xffffff,blurX:0,blurY:0}})
					}else if (e.currentTarget == rightArrow) {
						TweenMax.to(rightArrow["arrow"],0.5,{glowFilter:{color:0xffffff,blurX:0,blurY:0}})
					}
				break;
			}
		}
		public function LoadingImage(id:int, bimg:String, msg:String):void {
			if (!bLoading) {
				return;
			}
			bLoading = false;
			while (addImg.numChildren) addImg.removeChildAt(0);
			this.id = id;
			ModelLocator.playerId = id;
			if (ModelLocator.playerId == 0) {
				clickEnableByLeft(false);
			}else if (ModelLocator.playerId == ModelLocator.gridlistLength - 1) {
				clickEnableByRight(false);
			}else {
				clickEnableByLeft(true);
				clickEnableByRight(true);
			}
			msgPanel.alpha = 0;
			msgTxt.text = msg;
			
			lbc.alpha = lbv.alpha = 1;
			lbv.x = - imgWidth / 2;
			lbc.y = - imgHeight / 2 + 5;
			loader = new Loader();
			loader.unload();
			loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, __loaderProgressHandle);
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE, __loaderCompleteHandle);
			loader.load(new URLRequest(bimg));
		}
		
		private function __loaderProgressHandle(e:ProgressEvent):void 
		{
			var eNum:Number = e.bytesLoaded / e.bytesTotal;
			TweenMax.to(lbv, 0.5, { x:imgWidth * eNum - imgWidth / 2 } );
			TweenMax.to(lbc, 0.5, { y:imgHeight * eNum - imgHeight / 2 + 5 } );
		}
		
		private function __loaderCompleteHandle(e:Event):void 
		{
			loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, __loaderCompleteHandle);
			TweenMax.killChildTweensOf(this, true);
			bLoading = true;
			var picWidth:Number = loader.content.width;
			var picHeight:Number = loader.content.height;
			imgWidth = picWidth + 12;
			imgHeight = picHeight + 20;
			
			TweenMax.to(lbc, 0.5, { y:imgHeight / 2} );
			TweenMax.to(lbc, 0.3, { alpha:0 } );
			TweenMax.to(lbv, 0.5, { x:imgWidth / 2 - 5} );
			TweenMax.to(lbv, 0.5, { alpha:0 } );
			
			TweenMax.to(lbc, 0.5, { width:imgWidth } );
			TweenMax.to(lbv, 0.5, { height:imgHeight } );
			
			loader.x = -picWidth / 2;
			loader.y = -picHeight / 2;
			addImg.alpha = 0;
			addImg.addChild(loader);
			
			TweenMax.to(leftArrow, .8, { x:-picWidth / 2 - 35 } );
			TweenMax.to(rightArrow, .8, { x:picWidth / 2 + 35 } );
			TweenMax.to(addImg, .01, { width:picWidth, height:picHeight } );
			TweenMax.to(addImg, .5, { delay:0.5,alpha:1 } );
			TweenMax.to(imgBack, .5, { width:picWidth + 12, height:picHeight + 20 } );
			TweenMax.to(loadingMask, .5, { width:imgWidth, height:imgHeight } );
			TweenMax.to(msgPanel, .5, { x: -picWidth / 2, y:picHeight / 2,alpha:1 } );
		}
		private function __arrowClickEventHandle(e:MouseEvent):void 
		{
			if (!bLoading) {
				return;
			}
			//loader.unload();
			var facade:ApplicationFacade = ApplicationFacade.getInstance();
			switch(e.currentTarget) {
				case leftArrow:
					if (ModelLocator.playerId == 0) {
						return;
					}
					ModelLocator.playerId--;
					facade.sendNotification(ApplicationFacade.EVENT_DATA_HOMEPIC_POST);
				break;
				case rightArrow:
					if (this.id == ModelLocator.gridlistLength - 1) {
						return;
					}
					ModelLocator.playerId++;
					facade.sendNotification(ApplicationFacade.EVENT_DATA_HOMEPIC_POST);
				break;
			}
		}
		
		private function __mouseClickEventHandle(e:MouseEvent):void 
		{
			addImg.removeEventListener(MouseEvent.CLICK, __mouseClickEventHandle);
			imgBack.removeEventListener(MouseEvent.CLICK, __mouseClickEventHandle);
			ModelLocator.hsaPicDispaly = false;
			ModelLocator.ROOT.removeChild(this);
		}
		private function clickEnableByLeft(b:Boolean):void {
			if (b) {
				leftArrow.addEventListener(MouseEvent.ROLL_OVER, __arrowRollEventHandle);
				leftArrow.buttonMode = true;
				TweenMax.to(leftArrow, .5, { tint:0x000000 } );
			}else {
				leftArrow.removeEventListener(MouseEvent.ROLL_OVER, __arrowRollEventHandle);
				leftArrow.buttonMode = false;
				TweenMax.to(leftArrow, .5, { tint:0x999999 } );
				TweenMax.to(leftArrow["arrow"],0.5,{glowFilter:{color:0xffffff,blurX:0,blurY:0}})
			}
		}
		private function clickEnableByRight(b:Boolean):void {
			if (b) {
				rightArrow.addEventListener(MouseEvent.ROLL_OVER, __arrowRollEventHandle);
				rightArrow.buttonMode = true;
				TweenMax.to(rightArrow, .5, { tint:0x000000 } );
			}else {
				rightArrow.removeEventListener(MouseEvent.ROLL_OVER, __arrowRollEventHandle);
				rightArrow.buttonMode = false;
				TweenMax.to(rightArrow, .5, { tint:0x999999 } );
				TweenMax.to(rightArrow["arrow"],0.5,{glowFilter:{color:0xffffff,blurX:0,blurY:0}})
			}
		}
	}

}